package cn.licoy.wdog.common.util.pay;
/**
 * @Author Sunne
 * @Date 18-8-13上午9:53
 * @Param emp-hospital
 * 高山仰止,景行行止.虽不能至,心向往之。
 * ——————————————————————————女神保佑，永无bug——————————————————————
 **/

import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;

/**
 *                    .::::.
 *                  .::::::::.
 *                 :::::::::::  FUCK YOU
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 */


public class XmlJavaJson {

    public String xml2JSON(String xml) {
        return new XMLSerializer().read(xml).toString();
    }

    public String json2XML(String json) {
        JSONObject jobj = JSONObject.fromObject(json);
        String xml = new XMLSerializer().write(jobj);
        return xml;
    }

    /**
       * java对象转换为xml文件
       * @param load java对象.Class
       * @return    xml文件的String
       * @throws JAXBException
       */
    public String beanToXml(Object obj,Class<?> load) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(load);
        Marshaller marshaller = context.createMarshaller();
        // xml格式
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        //是否去掉报文头
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj,writer);
        return writer.toString();
    }

    /**
       * xml转换为对象
       * @param load    java对象.Class
       * @return    java对象
       * @throws JAXBException
       * @throws IOException
       */
    public Object xmlToBean(String xmlPath,Class<?> load) throws JAXBException, IOException {
        JAXBContext context = JAXBContext.newInstance(load);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Object object = unmarshaller.unmarshal(new File(xmlPath));
        return object;
    }

}
